package cn.hesion.leetcode;
/**
 * ClassName: LongestBeautifulSubstring <br/>
 * Description: <br/>
 * date: 2022/5/19 21:18<br/>
 *
 * @author Hesion<br />
 * @version
 * @since JDK 1.8
 */

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author: hesion
 * @create: 2022-05-19 21:18
 **/
public class LongestBeautifulSubstring {
    public int longestBeautifulSubstring(String word) {
        List<Character> windows = new ArrayList<>();
        Set<Character> set = new HashSet<>();
        int right = 0;
        int res = 0;
        int left = 0;
        while (right < word.length()) {
            if (windows.isEmpty() || word.charAt(right) >= windows.get(windows.size() - 1)) {
                char c = word.charAt(right);
                windows.add(c);
                set.add(c);
                if (set.size() == 5) {
                    res = Math.max(res, windows.size());
                }
            } else {
                windows.clear();
                set.clear();
                left = right;
                char c = word.charAt(left);
                windows.add(c);
                set.add(c);
            }
            ++right;
        }
        return res;
    }
}
